class Login {
    //微信小游戏中创建的隐藏的登录按钮，覆盖在界面上的“微信登录”按钮上
    public wxLoginBtn : any;

    constructor() {
        KBEngine.Event.register("onLoginSuccessfully", this, "OnLoginSuccessfully");
        KBEngine.Event.register("TryReloginBaseApp", this, "TryReloginBaseApp");
        KBEngine.Event.register("onReloginBaseappSuccessfully", this, "onReloginBaseappSuccessfully");
        KBEngine.Event.register("onReloginBaseappFailed", this, "onReloginBaseappFailed");
    }

    Login() {
        if (Laya.Browser.onWeiXin) {
            this.WxLogin();
        }
        else {
            this.TempLogin();
        }
    }

    OnLoginSuccessfully()
    {
        if (null != this.wxLoginBtn) {
            this.wxLoginBtn.hide();
        }
        
        Laya.stage.removeChildren();
        Laya.stage.addChild(Game.viewFarm);
        Laya.stage.addChild(Game.layerDialog);
        Game.layerDialog.addChild(Dialog.manager);
        Laya.stage.addChild(Game.layerPrompt);
        PlaySound.getPlaySound().playBg(1);
    }

    //微信登录
    WxLogin() {
        wx.getSetting({
            success: (resSetting) {
                if (true === resSetting.authSetting["scope.userInfo"]) {
                    //用户已经授权，直接获取用户相关信息
                    wx.login({
                        success: (resLogin) => {
                            if(resLogin.code) {
                                wx.getUserInfo({
                                    success: (resInfo) => {
                                        let userInfo = resInfo.userInfo;
                                        let datas = {};
                                        datas["platform"] = "h5_wx";
                                        datas["nickName"] = userInfo.nickName;
                                        datas["avatarUrl"] = userInfo.avatarUrl;
                                        datas["gender"] = userInfo.gender;
                                        datas["inviteId"] = wx.getLaunchOptionsSync().query.invite_id;
                                        datas["shareType"] = wx.getLaunchOptionsSync().query.share_type;
                                        let strDatas = ComFun.CreateDictString(datas);
                                        KBEngine.Event.fire("login", resLogin.code, "123456", strDatas);
                                    }
                                });
                            }
                        }
                    });
                }
                else {
                    //用户未授权、用户拒绝授权，显示登录按钮，等待玩家点击
                    this.CreateLoginBtn();
                }
            }
        })
    }

    //创建登录按钮
    CreateLoginBtn() {
        wx.getSystemInfo({
            success: (resSys) => {
                Game.viewStart.imgLoginwx.visible = true;
                let scalex = resSys.windowWidth / Laya.stage.designWidth;
                let scaley = resSys.windowHeight / Laya.stage.designHeight;
                let w = Game.viewStart.imgLoginwx.width * scalex * Game.viewStart.imgLoginwx.scaleX;
                let h = Game.viewStart.imgLoginwx.height * scaley * Game.viewStart.imgLoginwx.scaleY;
                let x = Game.viewStart.imgLoginwx.x * scalex;
                let y = Game.viewStart.imgLoginwx.y * scaley;
                this.wxLoginBtn = wx.createUserInfoButton({
                    type: "text",
                    text: "",
                    style: {
                        left: x,
                        top: y,
                        width: w,
                        height: h,
                        // color: '#000000',
                        // fontSize: 20,
                        // textAlign: 'center',
                        // borderWidth: 4,
                        // borderColor: "#000000"
                    }
                });
                
                this.wxLoginBtn.onTap((resBtn) => {
                    wx.login({
                        success: (resLogin) => {
                            if(resLogin.code) {
                                let userInfo = resBtn.userInfo;
                                if (null != userInfo) {
                                    let datas = {};
                                    datas["platform"] = "h5_wx";
                                    datas["nickName"] = userInfo.nickName;
                                    datas["avatarUrl"] = userInfo.avatarUrl;
                                    datas["gender"] = userInfo.gender;
                                    datas["inviteId"] = wx.getLaunchOptionsSync().query.invite_id;
                                    datas["shareType"] = wx.getLaunchOptionsSync().query.share_type;
                                    let strDatas = ComFun.CreateDictString(datas);
                                    KBEngine.Event.fire("login", resLogin.code, "123456", strDatas);
                                }
                            }
                        }
                    });
                });

                this.wxLoginBtn.show();
            }
        })
    }

    //游客登录
    TempLogin() {
        let datas = {};
        datas["platform"] = "h5_temp";
        let strDatas = ComFun.CreateDictString(datas);
        KBEngine.Event.fire("login", "wmhx0001", "123456", strDatas);
    }

    //重连baseapp
    TryReloginBaseApp() {
        KBEngine.app.reloginBaseapp();
    }

    //kbe重连baseapp成功时，通知服务器
    onReloginBaseappSuccessfully() {
        ComFun.BaseCall("reLoginSuccess");
    }

    //kbe重连baseapp失败时，重新走登录流程
    onReloginBaseappFailed(failedcode:any) {
        this.Login();
    }
}